package com.example.web.controller;

import com.example.common.utils.PreCondition;
import com.example.common.utils.result.BaseResponse;
import com.example.web.model.bo.DemoBO;
import com.example.web.model.vo.DemoVO;
import com.example.web.service.DemoService;
import com.example.web.template.ApiWebCallback;
import com.example.web.template.ApiWebTemplate;
import com.example.web.model.request.BaseReq;
import lombok.Data;
import lombok.experimental.Accessors;
import org.mapstruct.Mapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 *
 * </p>
 *
 * @author 12071
 * @date 2022/8/15 14:31
 */
@RestController
public class DemoController {

    // TODO: 2022/8/15 注入一些资源类, 如 Service, Manager, 转换类等

    @Resource
    private DemoService demoService;

    @Resource
    private DemoObjectConvert convert;


    @RequestMapping(method = RequestMethod.POST, value = "/api")
    public BaseResponse<DemoController.Response> apiMethod(@RequestBody DemoController.Request req){
        return ApiWebTemplate.INSTANCE().execute(
                new ApiWebCallback<Response>() {
                    @Override
                    public void check() {
                        PreCondition.checkArgument(req.getArg1(), "缺少 参数1");
                        PreCondition.checkArgument(req.getArg1()>=0, "参数1不能为负数");
                        PreCondition.checkArgument(req.getArg2(), "缺少 参数2");
                        PreCondition.checkArgument(req.getArg2().length() >=10, "参数2最短为5位");
                        PreCondition.checkArgument(req.getArg3(), "缺少 参数3");
                        PreCondition.checkArgument(req.getArg3().compareTo(BigDecimal.ONE)<0, "参数3不能超过1");
                    }

                    @Override
                    public Response callback() {
                        // TODO: 2022/8/15 执行业务方法
                        DemoBO demo = demoService.demoServiceMethod(convert.req2BO(req));
                        Response response=new Response();
                        return response.setVo(convert.bo2vo(demo));
                    }
                }, req);
    }



    @Data
    @Accessors(chain = true)
    public static class Request extends BaseReq {
        /**
         * 请求参数1
         */
        private Integer arg1;
        /**
         * 请求参数2
         */
        private String arg2;
        /**
         * 请求参数3
         */
        private BigDecimal arg3;
    }

    @Data
    @Accessors(chain = true)
    public static class Response {
        // TODO: 2022/8/15 返回的数据
        private DemoVO vo;
    }


    @Mapper(componentModel = "spring")
    public interface DemoObjectConvert{
        // TODO: 2022/8/15 请求类转换为业务类
        DemoBO req2BO(Request req);

        DemoVO bo2vo(DemoBO bo);
    }

}
